const host="127.0.0.1";
const WebAppBase ={
	configs:  {
    host: host,
    apiBase: "http://" + host + "/api",
    managerBase: "http://" + host + "/game/admin"
  	},
	apis: function(){
		return{
			login: this.configs.apiBase + "/common/login",
			check: this.configs.apiBase + "/common/check",
			logout: this.configs.apiBase + "/common/logout",
		}
	},
	htmls: function () {
        return{
            login: this.configs.managerBase + "/login.html",
            index: this.configs.managerBase + "/index.html",
        }
    }
};
let UserInfo = {
	userKey: "panda_user",
	set: function (value) {
        sessionStorage.setItem(this.userKey, JSON.stringify(value));
    },
	get: function(){
        let data = sessionStorage.getItem(this.userKey);
        if(data){
            data = JSON.parse(data);
            return{
            	name:data.name,
                nickName:data.nickName,
                mobile:data.mobile,
                address:data.address,
                sex:data.sex,
                idNo:data.idNo,
			}
		}
		return data;
	},
	clear: function () {
        sessionStorage.removeItem(this.userKey);
    }
};
const Index = {
	init: function () {
        Auth.check();
        this.initListener();
        let user = UserInfo.get();
        if(user){
			$("#topUserName").html(user.nickName);
            $("#leftUserName").html(user.nickName);
            $("#topUserInfo").html(user.nickName + "<small>"+user.address+"</small>");
		}
        console.log("Index init done.");
    },
    initListener: function () {
        $("#logout").click(function() {
            $.get(WebAppBase.apis().logout, function (data, status) {
                if (data) {
                    console.log("退出成功");
                    UserInfo.clear();
                    window.location.href = WebAppBase.htmls().login;
                    return;
                }
                console.log("服务端响应失败");
            });
        });
    },
};
const Auth = {
	check: function(){
		$.get(WebAppBase.apis().check,function (data,status) {
			if(data){
                if(data.code && data.code == "0"){
                    console.log("登录成功");
                    UserInfo.set(data);
                    return ;
                }
			}
			console.log("登录失败");
            UserInfo.clear();
            window.location.href = WebAppBase.htmls().login ;
            return ;
        });
	}
};

const Login = {
	initListener: function(){
		$("#loginSubmit").click(function(){
			$("#errorMsg").html("");
			let userName = $("#user").val();
			let pass = $("#pass").val();
			if (userName.length < 1) {
				$("#errorMsg").html("请输入用户名");
				return ;
			  }
			  if (pass.length < 1) {
				$("#errorMsg").html("请输入密码");
				return ;
			  }
			  let user ={
				name: userName,
				password: pass,
			  };
			  $.ajax({
				url : WebAppBase.apis().login,
					contentType : 'application/json;charset=UTF-8',
					type : "POST",
					dataType: 'json',
					async : true,
					data : JSON.stringify(user),
					cache : false,
					error : function(e){
						console.log(e);
					},
					success : function(data){
						if(data){
							if(data.code && data.code == "0"){
								window.location.href = WebAppBase.htmls().index ;
							}else{
								$("#errorMsg").html(data.msg);
							}
						}
					}
			  });
		});
	},
	init: function(){
		this.initListener();
		console.log("init done.");
	}
};